﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PhotoGallery.Abstract;
using System.Data.Linq;
using PhotoGallery.Entities;
using System.Web.Security;

namespace PhotoGallery.Concrete
{
    public class SqlOwnersRepository : IOwnersRepository
    {
        private Table<Owners> OwnerTable;
        MembershipUser currentUser;
        
        public SqlOwnersRepository(string connectionString)
        {
            OwnerTable = (new DataContext(connectionString)).GetTable<Owners>();
        }
       
        public IQueryable<Owners> Owners
        {
            get { return OwnerTable; }
        }

        
        public void SaveOwner(Owners owner)
        {
            currentUser = Membership.GetUser();
            if (owner.UserId == null)
            {
                currentUser = Membership.GetUser();
                owner.UserId = (Guid)currentUser.ProviderUserKey; 
                if (owner.AdminComment == null) owner.AdminComment = "";
                owner.RegistrationDate = currentUser.CreationDate;
                OwnerTable.InsertOnSubmit(owner);
            }
            else
            {
                OwnerTable.Attach(owner);
                OwnerTable.Context.Refresh(RefreshMode.KeepCurrentValues, owner);
            }
            OwnerTable.Context.SubmitChanges();
        }

    }
}